package team.seekers.hula.record.wind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import team.seekers.hula.common.annotation.ApiResponseBody;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.record.wind.entity.vo.QuestionSpaceListVO;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.record.wind.entity.dto.WindQueryDTO;
import team.seekers.hula.record.wind.entity.dao.WindDO;
import team.seekers.hula.record.wind.entity.vo.WindListVO;
import team.seekers.hula.record.wind.entity.vo.WindSpaceListVO;
import team.seekers.hula.record.wind.service.WindService;

import java.util.*;

/**
 * @author hbc
 * @since 2021-02-15
 */
@Controller
@ApiResponseBody
@RequestMapping("/wind")
public class WindController {

    @Autowired
    private WindService windService;

    @GetMapping("/space/list")
    public List<WindSpaceListVO> listAdminSpace(@RequestParam(required = false) String userId){
        if(!StringUtils.isNotBlank(userId)){
            userId = MindUtils.getUserId();
        }
        return windService.listAdminSpace(userId);
    }

    @GetMapping("/question/list")
    public List<QuestionSpaceListVO> listAdminQuestion(){
        return windService.listAdminQuestion(MindUtils.getUserId());
    }

    // 历史页的瀑布流
    @GetMapping("/pageMineOrderByTime")
    public PageResult<WindListVO> pageMineOrderByTime(WindQueryDTO queryDTO){
        queryDTO.setUserId(MindUtils.getUserId());
        return windService.pageOrderByTime(queryDTO);
    }

    // 历史页的瀑布流
    @GetMapping("/pageOrderByTime")
    public PageResult<WindListVO> pageOrderByTime(WindQueryDTO queryDTO){
        return windService.pageOrderByTime(queryDTO);
    }


    // 获取最近的输出状况
    @GetMapping("/recently-wind")
    public ArrayList<Integer> recentlyMind(){
        ArrayList<Integer> mindDeeps = new ArrayList<>();
        // 今天的状况
        Calendar todayCalendar = Calendar.getInstance();
        Date now = new Date(todayCalendar.getTimeInMillis());
        todayCalendar.set(Calendar.SECOND, 0);
        todayCalendar.set(Calendar.MINUTE, 0);
        todayCalendar.set(Calendar.HOUR_OF_DAY, 0);
        todayCalendar.set(Calendar.MILLISECOND, 0);
        Date zero = new Date(todayCalendar.getTimeInMillis());
        int mind = windService.count(new QueryWrapper<WindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", zero, now));
        mindDeeps.add(0,toMindDeep(mind));
        mind = windService.count(new QueryWrapper<WindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*1), zero));
        mindDeeps.add(0,toMindDeep(mind));
        mind = windService.count(new QueryWrapper<WindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*2), new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*1)));
        mindDeeps.add(0,toMindDeep(mind));
        mind = windService.count(new QueryWrapper<WindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*3), new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*2)));
        mindDeeps.add(0,toMindDeep(mind));
        mind = windService.count(new QueryWrapper<WindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*4), new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*3)));
        mindDeeps.add(0,toMindDeep(mind));
        mind = windService.count(new QueryWrapper<WindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*5), new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*4)));
        mindDeeps.add(0,toMindDeep(mind));
        mind = windService.count(new QueryWrapper<WindDO>().eq("user_id", MindUtils.getUserId()).between("gmt_create", new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*6), new Date(todayCalendar.getTimeInMillis()-1000*60*60*24*5)));
        mindDeeps.add(0,toMindDeep(mind));

        return mindDeeps;
    }

    public Integer toMindDeep(Integer mindNumber){
        if(mindNumber == 0){
            return 0;
        }
        if(mindNumber > 0 && mindNumber <= 5){
            return 1;
        }

        if(mindNumber > 5 && mindNumber <= 10){
            return 2;
        }
        if(mindNumber > 10){
            return 3;
        }
        return 0;
    }

}

